import qs from 'qs';
import auth from '@/utils/auth';
import ajax from '@/utils/ajax';
import apiConfig from '@/utils/apiConfig';

export default {
  state: {
    account: '',
    token: auth.getToken(),
    roles: []
  },

  mutations: {
    SET_ACCOUNT: (state, account) => {
      state.account = account;
    },
    SET_ACCOUNT_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_ACCOUNT_ROLES: (state, roles) => {
      state.roles = roles;
    }
  },

  actions: {
    loginByAccount({ commit }, data) {
      apiConfig.removeItem();
      return ajax.post('/backstage/auth/login', qs.stringify(data));
    },
    logoutAccount({ commit, state }) {
      commit('SET_ACCOUNT', '');
      commit('SET_ACCOUNT_TOKEN', '');
      commit('SET_ACCOUNT_ROLES', []);
      auth.removeToken();
    }
  }
};
